<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/12/6
 * Time: 17:00
 */
date_default_timezone_set('PRC');
//定时申请
//福利社红包 未登录只按条件查询
//           登录的按条件查询最新的，同时可以查询已扫的红包

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

$people_num=$_GET['people_num'];
$max_price=$_GET['max_price'];
$select_time=$_GET['select_time'];
$brief=$_GET['brief'];
$userid = $_GET['userid'];

if($people_num == null){
    $people_num = 200;
}
if($max_price == null){
    $max_price = 2;
}
if($select_time == null){
    $select_time = 10;
}

//得到福利社红包
function getFulishe($nowDt,$fulishe,$callback_result,$conn,$people_num,$max_price){
    $query = "select url,name,pic,people_num,min_price,max_price,actPrice,origPrice from juhuasuan where people_num < ".$people_num." and max_price >= ".(100*$max_price)
        ." and item_time = '$nowDt' order by people_num";
    $result = queryRecord($query,$conn);

    $mf_index = 0;//首页福利社一行显示三条，只显示一行
    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
        $fulishe .= '<div class="fulishe_baozi display_inline_block">';
        $fulishe .= '    <p>'.$row['name'].'</p>';
        $fulishe .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #ff5555">'.($row['min_price']/100).'-'.($row['max_price']/100).'元</div>';
        $fulishe .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #22ffff">'.$row['people_num'].'人</div>';
        $fulishe .= '    <div class="fulishe_condition_area display_inline_block"><input type="checkbox"/>已扫</div>';
        $fulishe .= '    <img src="../logo/qq.png" alt=""/>';
        $fulishe .= '</div>';
        if($mf_index<3){
            $main_fulishe = $fulishe;
            $mf_index++;
        }
    }
    $main_fulishe .= '<div class="fulishe_baozi display_inline_block">';
    $main_fulishe .= '    <p>新的包子</p>';
    $main_fulishe .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #ff5555">1-2元</div>';
    $main_fulishe .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #22ffff">100人</div>';
    $main_fulishe .= '    <div class="fulishe_condition_area display_inline_block"><input type="checkbox"/>已扫</div>';
    $main_fulishe .= '    <img src="../logo/qq.png" alt=""/>';
    $main_fulishe .= '</div>';
    $callback_result['m_fls'] = $main_fulishe;
    $callback_result['fls'] = $fulishe;

    return $callback_result;
}

//返回数据包含：福利社红包(key：fulishe)，已扫红包(key：hb_scanned)，白菜(key：baicai)，免费试用(key：free)，佣金任务(key：mission)
function getData($userid,$people_num=200,$max_price=200){
    $callback_result = array();

    //返回html
    $main_fulishe = ''; //首页fulishe
    $fulishe = '';      //福利社页面的红包区域
    $hb_scanned = '';   //福利社页面的已扫区域

    $conn = conn_mysql("jufuli");
    //如果用户未登录，则不能设置已扫红包
    //用户登录，如果不是vip，也不能设置已扫红包
    //如果用户是vip用户且登录，则查询出fulishe红包，已扫红包
    $nowDt = date("y-m-d",time());
    if($userid == null || $userid = ''){
        $callback_result['user_type'] = 0;//普通用户
        $callback_result['hb_scanned'] = '';

        $callback_result = getFulishe($nowDt,$fulishe,$callback_result,$conn,$people_num,$max_price);
    }else{
        $query = "select (case when '$nowDt' BETWEEN vip_start_time and vip_end_time then 1 when is_manage=1 then 2 else 0 end) as user_type from user_mn WHERE userid = '$userid' ; ";
        $result = queryRecord($query,$conn);
        $user_type = 0;
        while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
            $user_type = $row['user_type'];
        }
        if ($user_type > 0){
            $callback_result['user_type'] = 1;//普通用户
            $callback_result = getFulishe($nowDt,$fulishe,$callback_result,$conn,$people_num,$max_price);

            $query = "select a.url,a.name,a.pic,a.people_num,a.min_price,a.max_price,a.actPrice,a.origPrice from juhuasuan a JOIN scan_item b "
                    ." ON (a.itemid = b.itemid and b.userid = '$userid' and b.scan_time = '$nowDt')"
                    ." where a.item_time = '$nowDt' order by people_num";
            $result = queryRecord($query,$conn);
            while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
                $hb_scanned .= '<div class="fulishe_baozi display_inline_block">';
                $hb_scanned .= '    <p>'.$row['name'].'</p>';
                $hb_scanned .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #ff5555">'.($row['min_price']/100).'-'.($row['max_price']/100).'元</div>';
                $hb_scanned .= '    <div class="fulishe_condition_area display_inline_block" style="background-color: #22ffff">'.$row['people_num'].'人</div>';
                $hb_scanned .= '    <div class="fulishe_condition_area display_inline_block"><input type="checkbox" checked/>已扫</div>';
                $hb_scanned .= '    <img src="../logo/qq.png" alt=""/>';
                $hb_scanned .= '</div>';
            }
            $callback_result['hb_scanned'] = $hb_scanned;
        }else{
            $callback_result = getFulishe($nowDt,$fulishe,$callback_result,$conn,$people_num,$max_price);

            $callback_result['m_fls'] = $main_fulishe;
            $callback_result['fls'] = $fulishe;
        }
        $callback_result['user_type'] = $user_type;//普通用户
    }
    //得到今天包子数量
    $fls_num = 0;
    $query = "select count(1) as cnt from juhuasuan where item_time = '$nowDt'";
    $result = queryRecord($query,$conn);
    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
        $fls_num = $row['cnt'];
    }
    $callback_result['fls_num'] = $fls_num;
    //

    return $callback_result;
}

$callback_result = getData('');
echo json_encode($callback_result);